/** @format */
import { getToday } from '../getToday'

// login接口使用 当天第一次登陆有数据变更的时候使用
// updateFirstLoginToday({ $inc: { gameTime: 3}})
export default function updateUserInfo(update?: IPreUpdateQuery) {
  return async function (
    context: IContext<IParams>,
    { userInfo }: IControllerInfos
  ): Promise<IPreUpdateQuery | undefined> {
    if (!userInfo) return {}
    return update
  }
}

export function reduceGameTimes(needGameTimes: number) {
  return updateUserInfo({
    $where: `this.gameTimes >= ${needGameTimes}`,
    $inc: { gameTimes: -needGameTimes }
  })
}

export function reduceTool(toolKey: string, count: number) {
  return updateUserInfo({
    $where: `this.tools.${toolKey} >= ${count}`,
    $inc: { [`tools.${toolKey}`]: -count }
  })
}

export async function updateHelpRecord(
  context: IContext<IParams>,
  { userInfo }: IControllerInfos
): Promise<IPreUpdateQuery | undefined> {
  const { inviteId } = context.data
  if (!userInfo) return {}
  return {
    $set: {
      inviteId
    }
  }
}

export async function updateGrowHelpRecord(
  context: IContext<IParams>,
  { userInfo }: IControllerInfos
): Promise<IPreUpdateQuery | undefined> {
  const { inviteId, key } = context.data
  if (!userInfo || key !== 'invite' || !inviteId) return {}
  return {
    $set: {
      inviteId
    }
  }
}
